<?php

App::uses('AppController', 'Controller');

/**
 * Sociais Controller
 *
 * @property Social $Social
 * @property PaginatorComponent $Paginator
 */
class SociaisController extends SocialAppController {

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /** Funçoes Publicas * */
    /** Funçoes Administrador * */

    /**
     * painel_index method
     *
     * @return void
     */
    public function painel_index() {
        $this->Prg->encode = true;
        $this->Prg->commonProcess();
        $options = array(
            'conditions' => $this->{$this->modelClass}->parseCriteria($this->passedArgs),
            'order' => array($this->modelClass . '.created' => 'DESC'),
            'limit' => Configure::read('Configuracoes.PaginacaoPainel'),
        );

        $this->paginate = $options;
        ${$this->modelClass} = $this->paginate($this->modelClass);
        $this->set('title_for_layout', 'Social - Consultar');
        $this->set(Inflector::variable(Inflector::underscore(Inflector::pluralize($this->modelClass))), ${$this->modelClass});
    }

    /**
     * painel_add method
     *
     * @return void
     */
    public function painel_add() {
        if ($this->request->is('post')) {
            if ($this->{$this->modelClass}->save($this->request->data)) {
                $this->Session->setFlash(__('%s cadastrado com sucesso.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-success'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Não foi possível cadastrar o %s.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-danger'));
            }
        }

        $this->set('title_for_layout', 'Social - Cadastrar');
    }

    /**
     * painel_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function painel_edit($id = null) {
        if (!$this->{$this->modelClass}->exists($id)) {
            throw new NotFoundException(__('Invalid conteudo'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->{$this->modelClass}->save($this->request->data)) {
                $this->Session->setFlash(__('%s alterado com sucesso.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-success'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Não foi possível alterar o %s.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-danger'));
            }
        } else {
            $options = array('conditions' => array($this->modelClass . '.' . $this->{$this->modelClass}->primaryKey => $id));
            $this->request->data = $this->{$this->modelClass}->find('first', $options);
        }

        $this->set('title_for_layout', 'Social - Alterar');
    }

    /**
     * painel_delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function painel_delete($id = null) {
        $this->{$this->modelClass}->id = $id;
        if (!$this->{$this->modelClass}->exists()) {
            $this->Session->setFlash(__('%s invalido.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-danger'));
        } else {
            if ($this->{$this->modelClass}->delete($id)) {
                $this->Session->setFlash(__('%s excluido com sucesso.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-success'));
            } else {
                $this->Session->setFlash(__('Não foi possivel excluir o %s.', 'social'), 'alert', array('plugin' => 'BoostCake', 'class' => 'alert-danger'));
            }
        }
        $this->redirect($this->referer());
    }

}
